<template>
    <el-dialog
            :title="!dataForm.assignmentId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                <el-form-item label="分配方式" prop="assignmentType">
                    <el-select v-model="dataForm.assignmentType" placeholder="请选择分配方式">
                        <el-option label="自动" value="auto"></el-option>
                        <el-option label="手动" value="manual"></el-option>
                    </el-select>
                </el-form-item>   
                <el-form-item label="分配人" prop="assignedBy">
                        <el-input v-model="dataForm.assignedBy" placeholder="分配人"></el-input>
                </el-form-item>
                <el-form-item label="分配时间" prop="assignedAt">
                        <el-date-picker
                                v-model="dataForm.assignedAt"
                                type="datetime"
                                placeholder="分配时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="分配状态" prop="status">
                    <el-select v-model="dataForm.status" placeholder="请选择分配状态">
                        <el-option label="待处理" value="pending"></el-option>
                        <el-option label="已完成" value="completed"></el-option>
                        <el-option label="已取消" value="cancelled"></el-option>
                    </el-select>
                </el-form-item>   
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            assignmentId: null,
            planId: null,
            carrierId: null,
            driverId: null,
            assignmentType: null,
            assignedBy: null,
            assignedAt: null,
            status: null,
            createdAt: null,
            updatedAt: null
          },
          dataRule: {
            assignmentId: [
                        { required: true, message: '任务分配唯一标识不能为空', trigger: 'blur' }
            ],
            planId: [
                        { required: true, message: '关联运输计划ID不能为空', trigger: 'blur' }
            ],
            carrierId: [
                        { required: true, message: '分配的承运商ID不能为空', trigger: 'blur' }
            ],
            driverId: [
                        { required: true, message: '分配的司机ID不能为空', trigger: 'blur' }
            ],
            assignmentType: [
                        { required: true, message: '分配方式（自动、手动）不能为空', trigger: 'blur' }
            ],
            assignedBy: [
                        { required: true, message: '分配人（如果是手动分配）不能为空', trigger: 'blur' }
            ],
            assignedAt: [
                        { required: true, message: '分配时间不能为空', trigger: 'blur' }
            ],
            status: [
                        { required: true, message: '分配状态（待处理、已完成、已取消）不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.assignmentId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.assignmentId = id || 0
            if (this.dataForm.assignmentId) {
              this.$http({
                url: this.$http.adornUrl(`/app/task_assignments/info/${this.dataForm.assignmentId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.apptaskassignments
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/task_assignments/${!this.dataForm.assignmentId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>